package com.scommunity.uniapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Feedback;
import com.scommunity.domain.Owner;
import com.scommunity.domain.User;
import com.scommunity.service.IFeedbackService;
import com.scommunity.service.IOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@CrossOrigin
@RequestMapping("/user/feedback")
@RestController
public class UFeedBackController {
    @Autowired
    IFeedbackService feedbackService;
    @Autowired
    IOwnerService ownerService;
    @RequestMapping(value ="/addFb")
    public ResponseResult addFeedback(@SessionAttribute User user, @RequestBody Feedback feedback){
        Long ownerId = getOwnerId(user);
        feedback.setFbStatus("已接收");
        feedback.setOwnerId(ownerId);
        feedback.setFbTime(LocalDateTime.now());
        feedbackService.save(feedback);
        return  ResponseResult.success();
    }
    @RequestMapping(value = "/getMyFb")
    public ResponseResult getMyFb(  @SessionAttribute User user,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        //测试能否使用
       Long ownerId = getOwnerId(user);
       //Long ownerId=2L;
        PageInfo<Feedback> list = feedbackService.getFeedbacksByPage(pageNum, pageSize, ownerId);
        return  ResponseResult.success(list);
    }
    public Long getOwnerId(User user){
        Long userId = user.getUserId();
        Owner owner = ownerService.getOne(new QueryWrapper<Owner>().eq("user_id", userId));
        Long ownerId = owner.getOwnerId();
        return ownerId;
    }
}
